load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

licenses(["notice"])

exports_files(["LICENSE"])

COMMON_COPTS = select({
    "@platforms//os:windows": [],
    "//conditions:default": ["-w"],
})

COMMON_DEFINES = [
    "CFG_RUNTIME_DLLFILE=\\\"\"Tcl\\\"\"",
    "HAVE_TCL_CONFIG_H=1",
    "MODULE_SCOPE='extern __attribute__((__visibility__(\"hidden\")))'",
    "MP_FIXED_CUTOFFS",
    "PACKAGE_BUGREPORT=\\\"\"\\\"\"",
    "PACKAGE_URL=\\\"\"\\\"\"",
    "TCL_LIBRARY=\\\"\"\\\"\"",
    "TCL_PACKAGE_PATH=\\\"\"\\\"\"",
] + select({
    "@platforms//os:macos": [
        "MAC_OSX_TCL=1",
    ],
    "//conditions:default": [],
})

TCL_DEFINES = COMMON_DEFINES + [
    "PACKAGE_NAME=\\\"\"tcl\\\"\"",
    "PACKAGE_STRING=\\\"\"tcl 8.6\\\"\"",
    "PACKAGE_TARNAME=\\\"\"tcl\\\"\"",
    "PACKAGE_VERSION=\\\"\"8.6\\\"\"",
    "CFG_RUNTIME_BINDIR=\\\"\"/usr/local/bin\\\"\"",
    "CFG_RUNTIME_DLLFILE=\\\"\"Tcl\\\"\"",
    "CFG_RUNTIME_DOCDIR=\\\"\"/usr/local/man\\\"\"",
    "CFG_RUNTIME_INCDIR=\\\"\"\\\"\"",
    "CFG_RUNTIME_LIBDIR=\\\"\"\\\"\"",
    "CFG_RUNTIME_SCRDIR=\\\"\"\\\"\"",
    "CFG_INSTALL_BINDIR=\\\"\"\\\"\"",
    "CFG_INSTALL_DOCDIR=\\\"\"\\\"\"",
    "CFG_INSTALL_INCDIR=\\\"\"\\\"\"",
    "CFG_INSTALL_LIBDIR=\\\"\"\\\"\"",
    "CFG_INSTALL_SCRDIR=\\\"\"\\\"\"",
]

[
    copy_file(
        name = src.replace(".h", "_copy"),
        src = src,
        out = dest,
        tags = ["manual"],
    )
    for src, dest in {
        "tclConfig_macos.h": "macosx/tclConfig.h",
        "tclConfig_unix.h": "unix/tclConfig.h",
        "tclConfig_windows.h": "windows/tclConfig.h",
    }.items()
]

filegroup(
    name = "tclConfig",
    srcs = select({
        "@platforms//os:macos": ["macosx/tclConfig.h"],
        "@platforms//os:windows": ["windows/tclConfig.h"],
        "//conditions:default": ["unix/tclConfig.h"],
    }),
)

filegroup(
    name = "tclAppInit",
    srcs = select({
        "@platforms//os:windows": ["windows/tclAppInit.c"],
        "//conditions:default": ["unix/tclAppInit.c"],
    }),
)

filegroup(
    name = "all_headers",
    srcs = glob(
        include = ["**/*.h"],
        exclude = [
            "bazel-*/**",
            ".git/**",
        ],
    ) + [
        "generic/regc_color.c",
        "generic/regc_cvec.c",
        "generic/regc_lex.c",
        "generic/regc_locale.c",
        "generic/regc_nfa.c",
        "generic/rege_dfa.c",
        "generic/tcl.decls",
        "generic/tclInt.decls",
        "generic/tclOO.decls",
        "generic/tclTomMath.decls",
        "generic/tclUniData.c",
        ":tclConfig",
    ],
)

cc_library(
    name = "cc_all_headers",
    hdrs = [":all_headers"],
    includes = select({
        "@platforms//os:macos": ["macosx"],
        "@platforms//os:windows": ["windows"],
        "//conditions:default": ["unix"],
    }),
)

filegroup(
    name = "tcl_core",
    srcs = glob(["library/**"]),
    visibility = ["//visibility:public"],
)

alias(
    name = "tcl",
    actual = ":Tcl",
    visibility = ["//visibility:public"],
)

cc_library(
    name = "Tcl",
    srcs = [
        "generic/regcomp.c",
        "generic/regerror.c",
        "generic/regexec.c",
        "generic/regfree.c",
        "generic/tclAlloc.c",
        "generic/tclAssembly.c",
        "generic/tclAsync.c",
        "generic/tclBasic.c",
        "generic/tclBinary.c",
        "generic/tclCkalloc.c",
        "generic/tclClock.c",
        "generic/tclCmdAH.c",
        "generic/tclCmdIL.c",
        "generic/tclCmdMZ.c",
        "generic/tclCompCmds.c",
        "generic/tclCompCmdsGR.c",
        "generic/tclCompCmdsSZ.c",
        "generic/tclCompExpr.c",
        "generic/tclCompile.c",
        "generic/tclConfig.c",
        "generic/tclDate.c",
        "generic/tclDictObj.c",
        "generic/tclDisassemble.c",
        "generic/tclEncoding.c",
        "generic/tclEnsemble.c",
        "generic/tclEnv.c",
        "generic/tclEvent.c",
        "generic/tclExecute.c",
        "generic/tclFCmd.c",
        "generic/tclFileName.c",
        "generic/tclGet.c",
        "generic/tclHash.c",
        "generic/tclHistory.c",
        "generic/tclIO.c",
        "generic/tclIOCmd.c",
        "generic/tclIOGT.c",
        "generic/tclIORChan.c",
        "generic/tclIORTrans.c",
        "generic/tclIOSock.c",
        "generic/tclIOUtil.c",
        "generic/tclIndexObj.c",
        "generic/tclInterp.c",
        "generic/tclLink.c",
        "generic/tclListObj.c",
        "generic/tclLiteral.c",
        "generic/tclLoad.c",
        "generic/tclMain.c",
        "generic/tclNamesp.c",
        "generic/tclNotify.c",
        "generic/tclOO.c",
        "generic/tclOOBasic.c",
        "generic/tclOOCall.c",
        "generic/tclOODefineCmds.c",
        "generic/tclOOInfo.c",
        "generic/tclOOMethod.c",
        "generic/tclOOStubInit.c",
        "generic/tclObj.c",
        "generic/tclOptimize.c",
        "generic/tclPanic.c",
        "generic/tclParse.c",
        "generic/tclPathObj.c",
        "generic/tclPipe.c",
        "generic/tclPkg.c",
        "generic/tclPkgConfig.c",
        "generic/tclPosixStr.c",
        "generic/tclPreserve.c",
        "generic/tclProc.c",
        "generic/tclRegexp.c",
        "generic/tclResolve.c",
        "generic/tclResult.c",
        "generic/tclScan.c",
        "generic/tclStrToD.c",
        "generic/tclStringObj.c",
        "generic/tclStubInit.c",
        "generic/tclThread.c",
        "generic/tclThreadAlloc.c",
        "generic/tclThreadJoin.c",
        "generic/tclThreadStorage.c",
        "generic/tclTimer.c",
        "generic/tclTomMathInterface.c",
        "generic/tclTrace.c",
        "generic/tclUtf.c",
        "generic/tclUtil.c",
        "generic/tclVar.c",
        "generic/tclZlib.c",
        "libtommath/bn_mp_add.c",
        "libtommath/bn_mp_add_d.c",
        "libtommath/bn_mp_and.c",
        "libtommath/bn_mp_clamp.c",
        "libtommath/bn_mp_clear.c",
        "libtommath/bn_mp_clear_multi.c",
        "libtommath/bn_mp_cmp.c",
        "libtommath/bn_mp_cmp_d.c",
        "libtommath/bn_mp_cmp_mag.c",
        "libtommath/bn_mp_cnt_lsb.c",
        "libtommath/bn_mp_copy.c",
        "libtommath/bn_mp_count_bits.c",
        "libtommath/bn_mp_div.c",
        "libtommath/bn_mp_div_2.c",
        "libtommath/bn_mp_div_2d.c",
        "libtommath/bn_mp_div_d.c",
        "libtommath/bn_mp_exch.c",
        "libtommath/bn_mp_expt_n.c",
        "libtommath/bn_mp_grow.c",
        "libtommath/bn_mp_init.c",
        "libtommath/bn_mp_init_copy.c",
        "libtommath/bn_mp_init_multi.c",
        "libtommath/bn_mp_init_set.c",
        "libtommath/bn_mp_init_size.c",
        "libtommath/bn_mp_lshd.c",
        "libtommath/bn_mp_mod.c",
        "libtommath/bn_mp_mod_2d.c",
        "libtommath/bn_mp_mul.c",
        "libtommath/bn_mp_mul_2.c",
        "libtommath/bn_mp_mul_2d.c",
        "libtommath/bn_mp_mul_d.c",
        "libtommath/bn_mp_neg.c",
        "libtommath/bn_mp_or.c",
        "libtommath/bn_mp_pack.c",
        "libtommath/bn_mp_pack_count.c",
        "libtommath/bn_mp_radix_size.c",
        "libtommath/bn_mp_radix_smap.c",
        "libtommath/bn_mp_read_radix.c",
        "libtommath/bn_mp_rshd.c",
        "libtommath/bn_mp_set.c",
        "libtommath/bn_mp_shrink.c",
        "libtommath/bn_mp_signed_rsh.c",
        "libtommath/bn_mp_sqr.c",
        "libtommath/bn_mp_sqrt.c",
        "libtommath/bn_mp_sub.c",
        "libtommath/bn_mp_sub_d.c",
        "libtommath/bn_mp_to_radix.c",
        "libtommath/bn_mp_to_ubin.c",
        "libtommath/bn_mp_ubin_size.c",
        "libtommath/bn_mp_unpack.c",
        "libtommath/bn_mp_xor.c",
        "libtommath/bn_mp_zero.c",
        "libtommath/bn_s_mp_add.c",
        "libtommath/bn_s_mp_balance_mul.c",
        "libtommath/bn_s_mp_div_3.c",
        "libtommath/bn_s_mp_karatsuba_mul.c",
        "libtommath/bn_s_mp_karatsuba_sqr.c",
        "libtommath/bn_s_mp_mul_digs.c",
        "libtommath/bn_s_mp_mul_digs_fast.c",
        "libtommath/bn_s_mp_reverse.c",
        "libtommath/bn_s_mp_sqr.c",
        "libtommath/bn_s_mp_sqr_fast.c",
        "libtommath/bn_s_mp_sub.c",
        "libtommath/bn_s_mp_toom_mul.c",
        "libtommath/bn_s_mp_toom_sqr.c",
    ] + select({
        "@platforms//os:macos": [
            "macosx/tclMacOSXBundle.c",
            "macosx/tclMacOSXFCmd.c",
            "macosx/tclMacOSXNotify.c",
        ],
        "//conditions:default": [],
    }) + select({
        "@platforms//os:windows": [],
        "//conditions:default": [
            "unix/tclLoadDyld.c",
            "unix/tclUnixChan.c",
            "unix/tclUnixCompat.c",
            "unix/tclUnixEvent.c",
            "unix/tclUnixFCmd.c",
            "unix/tclUnixFile.c",
            "unix/tclUnixInit.c",
            "unix/tclUnixNotfy.c",
            "unix/tclUnixPipe.c",
            "unix/tclUnixSock.c",
            "unix/tclUnixThrd.c",
            "unix/tclUnixTime.c",
        ],
    }),
    copts = COMMON_COPTS,
    includes = [
        "generic",
        "libtommath",
    ] + select({
        "@platforms//os:macos": ["macosx"],
        "//conditions:default": [],
    }) + select({
        "@platforms//os:windows": [],
        "//conditions:default": ["unix"],
    }),
    local_defines = TCL_DEFINES,
    textual_hdrs = [":all_headers"],
    visibility = ["//visibility:public"],
    deps = [
        "@zlib",
    ],
)

cc_binary(
    name = "tclsh",
    srcs = [":tclAppInit"],
    copts = COMMON_COPTS,
    includes = [
        "generic",
        "libtommath",
    ] + select({
        "@platforms//os:windows": [],
        "//conditions:default": [
            "unix",
        ],
    }),
    linkopts = select({
        "@platforms//os:windows": [],
        "//conditions:default": [
            "-lpthread",
            "-ldl",
        ],
    }),
    local_defines = TCL_DEFINES,
    visibility = ["//visibility:public"],
    deps = [
        ":cc_all_headers",
        ":tcl",
        "@zlib",
    ],
)
